package net.zzy.pbbot.plugin.todo.api;

import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotPlugin;
import net.lz1998.pbbot.utils.Msg;
import net.zzy.pbbot.plugin.todo.service.TodoService;
import onebot.OnebotEvent;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static net.zzy.pbbot.finalfield.Command.TODO;
import static net.zzy.pbbot.finalfield.FinalStr.TODO_HELP;

/**
 * @author zhuZhaoYang
 * @date 2021/11/29 13:13
 */
@Component
public class Todo extends BotPlugin {
    private TodoService todoService;

    @Autowired
    public void setTodoService(TodoService todoService) {
        this.todoService = todoService;
    }

    @Override
    public int onGroupMessage(@NotNull Bot bot, @NotNull OnebotEvent.GroupMessageEvent event) {
        String rawMessage = event.getRawMessage();
        if (!rawMessage.startsWith(TODO)) {
            return MESSAGE_IGNORE;
        }
        String[] messages = rawMessage.split(" ");
        Long userId = event.getUserId();
        Msg msg = null;
        if (messages.length == 1 && messages[0].equals(TODO)) {
            msg = todoService.getTodo(userId);
        }
        if (messages.length == 2 && messages[0].equals(TODO) && "finished".equals(messages[1])) {
            msg = todoService.getFinishedTodo(userId);
        }
        if (messages.length == 2 && messages[0].equals(TODO) && "help".equals(messages[1])) {
            msg = Msg.builder().text(TODO_HELP);
        }
        if (messages.length >= 3 && messages[0].equals(TODO) && "add".equals(messages[1])) {
            StringBuilder content = new StringBuilder();
            for (int i = 2; i < messages.length; i++) {
                content.append(messages[i]);
            }
            msg = todoService.addTodo(userId, content.toString());
        }
        if (messages.length == 3 && messages[0].equals(TODO) && "finish".equals(messages[1])) {
            Integer todoId = Integer.valueOf(messages[2]);
            msg = todoService.finishTodo(userId, todoId);
        }
        if (messages.length == 3 && messages[0].equals(TODO) && "copy".equals(messages[1])) {
            Long sourceId = Long.valueOf(messages[2]);
            msg = todoService.copyTodo(userId, sourceId);
        }
        if (messages.length >= 4 && messages[0].equals(TODO) && "mod".equals(messages[1])) {
            Integer todoId = Integer.valueOf(messages[2]);
            StringBuilder content = new StringBuilder();
            for (int i = 3; i < messages.length; i++) {
                content.append(messages[i]);
            }
            msg = todoService.modTodo(userId, todoId, content.toString());
        }
        if (msg == null) {
            bot.sendGroupMsg(event.getGroupId(), "发送格式有误，请参照以下格式:\n" + TODO_HELP, false);
            return MESSAGE_BLOCK;
        }
        bot.sendGroupMsg(event.getGroupId(), msg, false);
        return MESSAGE_BLOCK;
    }

    @Override
    public int onPrivateMessage(@NotNull Bot bot, @NotNull OnebotEvent.PrivateMessageEvent event) {
        String rawMessage = event.getRawMessage();
        if (!rawMessage.startsWith(TODO)) {
            return MESSAGE_IGNORE;
        }
        String[] messages = rawMessage.split(" ");
        Long userId = event.getUserId();
        Msg msg = null;
        if (messages.length == 1 && messages[0].equals(TODO)) {
            msg = todoService.getTodo(userId);
        }
        if (messages.length == 2 && messages[0].equals(TODO) && "finished".equals(messages[1])) {
            msg = todoService.getFinishedTodo(userId);
        }
        if (messages.length == 2 && messages[0].equals(TODO) && "help".equals(messages[1])) {
            msg = Msg.builder().text(TODO_HELP);
        }
        if (messages.length >= 3 && messages[0].equals(TODO) && "add".equals(messages[1])) {
            StringBuilder content = new StringBuilder();
            for (int i = 2; i < messages.length; i++) {
                content.append(messages[i]);
            }
            msg = todoService.addTodo(userId, content.toString());
        }
        if (messages.length == 3 && messages[0].equals(TODO) && "finish".equals(messages[1])) {
            Integer todoId = Integer.valueOf(messages[2]);
            msg = todoService.finishTodo(userId, todoId);
        }
        if (messages.length == 3 && messages[0].equals(TODO) && "copy".equals(messages[1])) {
            Long sourceId = Long.valueOf(messages[2]);
            msg = todoService.copyTodo(userId, sourceId);
        }
        if (messages.length >= 4 && messages[0].equals(TODO) && "mod".equals(messages[1])) {
            Integer todoId = Integer.valueOf(messages[2]);
            StringBuilder content = new StringBuilder();
            for (int i = 3; i < messages.length; i++) {
                content.append(messages[i]);
            }
            msg = todoService.modTodo(userId, todoId, content.toString());
        }
        if (msg == null) {
            bot.sendPrivateMsg(userId, "发送格式有误，请参照以下格式:\n" + TODO_HELP, false);
            return MESSAGE_BLOCK;
        }
        bot.sendPrivateMsg(userId, msg, false);
        return MESSAGE_BLOCK;
    }
}
